//
//  TYHSMSVerifyController.m
//  OpenPractice
//
//  Created by tanyuehong on 9/7/18.
//  Copyright © 2018年  lijinghua. All rights reserved.
//

#import "UIImage+Tint.h"
#import <SMS_SDK/SMSSDK.h>
#import "UIView+Gesture.h"
#import "UIView+LineView.h"
#import <pop/POPSpringAnimation.h>
#import "POPSpringAnimation.h"
#import "TYHSMSVerifyController.h"
#import "UIButton+backgroundColorForState.h"

#define maxVerifyCodeCount  4
#define KTextFieldHeight    50
#define LABELFONT   [UIFont systemFontOfSize:(DeviceIsPad ? 14 : 12)]

@interface TYHSMSVerifyController () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView  *headerView;    // 表格头部
@property (nonatomic, strong) UILabel *codeErrorLabel;      // 验证码位数错误
@property (nonatomic, strong) UIView  *codeErrorView;       // 验证码错误_移动的view
@property (nonatomic, strong) UIButton *sendCodeBtn;        // 60秒后重发
@property (nonatomic, strong) NSMutableArray *showLabelArr;
@property (nonatomic, strong) UITextField *codeTextField;   // 验证码
@property (nonatomic, strong) UIButton *noReceivedBtn;      // 没有收到
@property (nonatomic, strong) UILabel  *pwErrorLabel;       // 密码位数错误
@property (nonatomic, strong) UIButton *registerBtn;        // 注册btn
@property (nonatomic, strong) UIView *moveView;             // 没有收到_移动的view;
@property (nonatomic, strong) UITextField *inviteCodeTextField; // 邀请码
@property (nonatomic, strong) UILabel *setGuideLabel;           // 设置密码后
@property (nonatomic, strong) UITextField *passwordTextField;   // 设置密码
@property (nonatomic, strong) UIButton *clearButton1;
@property (nonatomic, strong) UIButton *clearButton2;
@property (nonatomic, strong) UILabel      *resultLable;
@property (nonatomic, strong) UITextField  *secondPassworldTextField;///<重新输入

@property (nonatomic, assign) BOOL           pwErrorMark;///<密码位数不对, 或者没有收到验证码 YES有错 NO 没错
@property (nonatomic, assign) BOOL           codeErrorMark;///<记录验证码错误 YES有错 NO没错
@property (nonatomic, assign) CGRect         oldMoveViewFrame;
@property (nonatomic, assign) CGRect         oldCodeErrorFrame;

@end

@implementation TYHSMSVerifyController

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setUpNavigationBar];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.type == SMSVerifyREGISTERTYPE || self.type == SMSVerifyCodeLoginType) { // 验证码注册、验证码登录显示返回按钮
        //[self setDefaultBackItemWithBackArrow];
    } else {    // 隐藏返回按钮
       // [self setLeftItemImage:nil selectedImage:nil target:nil action:nil];
    }
    if (self.type == SMSVerifyREGISTERTYPE || self.type == SMSVerifyCodeLoginType) {
        BOOL isNeedSend = [[TYHLoginManager sharedInstance] checkUserPhoneNumSendSms:self.phoneNum secondBlock:^(NSInteger secodCount) {
            [self secondCountUpdate:secodCount];
        }];
        if (isNeedSend) {
            [self sendSmsCode];
        }
        [self addRegisterLabel];
        [self addMoveView];
    } else if (self.type== SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW) {
       // [self setRightItemText:@"跳过" Color:0x333333 target:self action:@selector(jumpAction)];
        [_registerBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self addRegisteSuccessLabel];
        [self addMoveView];
    }
    [self addNotification];
}

// 跳过操作
- (void)jumpAction {
    if (self.type == SMSVerifyVERITYTYPE) {
        // [[LoginManager sharedInstance] setNeedResetPasswrd:@"yes"];
        // [[LoginManager sharedInstance] saveLoginEntity];
        // [self jumpToFillRegister];
    } else if (self.type == SMSVerifyCodeLoginNeedSetPW) {
        // [[LoginManager sharedInstance] setNeedResetPasswrd:@"yes"];
        // [[LoginManager sharedInstance] saveLoginEntity];
        // [[LoginManager sharedInstance] fetchUserInfo];
    }
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


- (void)addRegisteSuccessLabel {
    self.resultLable = [[UILabel alloc] initWithFrame:CGRectMake(40, kScreenHeight == 480 ? 15.f : 88.f, 180, 26)];
    self.resultLable.textColor = kMainStyle1FontColor;
    self.resultLable.font = [UIFont systemFontOfSize:26];
    if (self.type == SMSVerifyVERITYTYPE) {
        self.resultLable.text = @"注册成功";
    } else if (self.type == SMSVerifyCodeLoginNeedSetPW) {
        self.resultLable.text = @"登录成功";
    }
    [self.headerView addSubview:self.resultLable];
    
    // 标题
    self.setGuideLabel = [[UILabel alloc]init];
    [self.setGuideLabel setFont:LABELFONT];
    [self.setGuideLabel setFrame:CGRectMake(40, self.resultLable.bottom + 10, kScreenWidth - 30, 30)];
    self.setGuideLabel.backgroundColor = [ UIColor whiteColor];
    [self.setGuideLabel setTextColor:[UIColor colorWithHex:0x999999] ];
    [self.setGuideLabel setTextAlignment:NSTextAlignmentLeft];
    [self.setGuideLabel setText:@"设置密码后，登陆更加安全与方便。"];
    [self.setGuideLabel sizeToFit];
    [self.headerView addSubview:self.setGuideLabel];
}

- (void)setUpNavigationBar {
 //   UIBarButtonItem *leftItem = [self barWithImage:[UIImage imageNamed:@"nav_bar_back.png"] selectedImage:[UIImage imageNamed:@"nav_bar_back_pressed.png"] backgroundImage:nil selectedBackgroundImage:nil target:self action:@selector(backClick:)];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)?-20:-10;
  //  [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, leftItem, nil]];
    
    [self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.customView isKindOfClass:[UIButton class]]) {
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back.png"] imageWithTintColor:[UIColor colorWithHex:0xffffff]] forState:UIControlStateNormal];
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back_pressed.png"] imageWithTintColor:[UIColor colorWithHex:0xdcf4fc]]  forState:UIControlStateSelected];
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back_pressed.png"] imageWithTintColor:[UIColor colorWithHex:0xdcf4fc]]  forState:UIControlStateHighlighted];
        }
    }];
}

- (void)addNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];
    if (self.type == SMSVerifyREGISTERTYPE || self.type == SMSVerifyCodeLoginType) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTextField:) name:UITextFieldTextDidChangeNotification object:_codeTextField];
    }
}

- (void)sendSmsCode {
    if (!self.phoneNum || self.phoneNum.length == 0) {
       // [self showMessageOnlyTip:@"手机号出错了哦！" withTipType:VCShowTipTypeError];
        return ;
    }
//    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNum zone:@"86"  result:^(NSError *error) {
//        if (error) {
//            //[self showMessageOnlyTip:@"获取短信失败" withTipType:VCShowTipTypeError];
//        }
//    }];
}

- (void)confirmBtnClick:(UIButton *)btn {
    if (self.type == SMSVerifyREGISTERTYPE || self.type == SMSVerifyCodeLoginType) {
        if (self.codeTextField.text.length<4) {
           // [self showMessageOnlyTip:@"请输入验证码" withTipType:VCShowTipTypeError];
            return;
        }
        [self verifySMSCodeWithCode:self.codeTextField.text];
    } else if (self.type == SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW) {
        if (self.passwordTextField.text.length < 6) {
            //[self showMessageOnlyTip:@"密码位数错误，请输入6-14位密码" withTipType:VCShowTipTypeError];
            return;
        }
        if (![self.passwordTextField.text isEqualToString:self.secondPassworldTextField.text]) {
           // [self showMessageOnlyTip:@"两次密码输入不一致，请重新输入" withTipType:VCShowTipTypeError];
            return;
        }
    }
}

- (void)verifySMSCodeWithCode:(NSString *)code {
//    [self startAnimatingWithText:@"验证中，请稍后"];
    [[TYHLoginManager sharedInstance] regiserWithPhoneCode:self.phoneNum code:code zone:@"86" compeletBlock:^(NSError *error, id resultObject) {
     //   [self stopAnimating];
        if (!error) {
            if ([[resultObject objectForKey:@"needSetPwd"] integerValue]==1) {
                [self gotoSetPassworkController];
            } else {
               // NSString *successTips = [resultObject objectForKey:@"successTips"];
                // [self showToaseWithWindow:successTips];
            }
        }
    }];
}

- (void)gotoSetPassworkController {
    TYHSMSVerifyController *setPwd = [[TYHSMSVerifyController alloc] init];
    setPwd.type = SMSVerifyCodeLoginNeedSetPW;
    [self.navigationController pushViewController:setPwd animated:YES];
    NSArray *viewControlers = self.navigationController.viewControllers;
    if (viewControlers.count>0) {
        NSMutableArray *changs = [NSMutableArray arrayWithArray:viewControlers];
        [changs removeObject:self];
        self.navigationController.viewControllers = changs.copy;
    }
}

- (void)secondCountUpdate:(NSInteger)secodCount {
    NSString *str = [NSString stringWithFormat:@"%ld秒后重发",secodCount];
    if (secodCount==0) {
        [self.sendCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
        [_sendCodeBtn setTitleColor:kThemeGreenColor  forState:UIControlStateNormal];
        [self setSendCodeBtnStatus:YES];
    } else {
        [self.sendCodeBtn setTitle:str forState:UIControlStateNormal];
    }
}

// 添加注册控件
- (void)addRegisterLabel {
    // 如果是注册界面
    UILabel *inputLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kScreenHeight == 480 ? 15.f : 88.f, kScreenWidth - 80, 30)];
    inputLabel.textColor = [UIColor colorWithHex:0x333333];
    inputLabel.font = [UIFont systemFontOfSize:26];
    inputLabel.text = @"输入验证码";
    [inputLabel sizeToFit];
    [self.headerView addSubview:inputLabel];
    
    // 标题
    UILabel *guideLabel = [[UILabel alloc] init];
    [guideLabel setFont:LABELFONT];
    [guideLabel setFrame:CGRectMake(40, inputLabel.bottom + 10, kScreenWidth - 80, 30)];
    guideLabel.backgroundColor = [ UIColor whiteColor];
    [guideLabel setTextColor:[UIColor colorWithHex:0x333333] ];
    [guideLabel setTextAlignment:NSTextAlignmentLeft];
    [guideLabel setText:@"验证码已发送，请注意查收"];
    [guideLabel sizeToFit];
    [self.headerView addSubview:guideLabel];
    
    self.codeErrorLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, guideLabel.bottom + 10, kScreenWidth - 80, 20)];
    self.codeErrorLabel.text = @"验证码错误，请重新输入";
    [self.codeErrorLabel setFont:LABELFONT];
    self.codeErrorLabel.textColor = [UIColor colorWithHex:0xff3c37];
    [self.codeErrorLabel setBackgroundColor:[UIColor whiteColor]];
    [self.headerView addSubview: self.codeErrorLabel];
    
    self.codeErrorView = [[UIView alloc]initWithFrame:CGRectMake(0, guideLabel.bottom + 10, kScreenWidth, 337)];
    
    [self.codeErrorView setBackgroundColor:[UIColor whiteColor]];
    [self.headerView addSubview:self.codeErrorView];
    
    // 添加四个验证码的框
    float labelWidth = (kScreenWidth - 30 - 30 - 96) / maxVerifyCodeCount;
    _showLabelArr = [[NSMutableArray alloc] init];
    for (int i = 0;  i < maxVerifyCodeCount; i++) {
        UILabel *label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:24]];
        [label setFrame:CGRectMake(40 + labelWidth * i, 0, labelWidth - 10, KTextFieldHeight)];
        [label setLineWithDirection:TGLINEBOTTOM Color:[UIColor colorWithHex:0xe5e5e5] Height:kTYHSingleLine Width:0];
        label.userInteractionEnabled = YES;
        [label addTapGestureWithTarget:self selector:@selector(changeKeyBoard)];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setTextColor:kThemeGreenColor];
        [self.codeErrorView addSubview:label];
        [_showLabelArr addObject:label];
    }
    
    // 输入验证码
    _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, kScreenWidth - 30 - 30 - 97, KTextFieldHeight)];
    _codeTextField.delegate = self;
    [_codeTextField setFont:[UIFont systemFontOfSize:16]];
    _codeTextField.placeholder = @"请输入验证码";
    
    [_codeTextField setFont:[UIFont systemFontOfSize:16]];
    [_codeTextField setKeyboardType:UIKeyboardTypeNumberPad];
    [self.codeErrorView addSubview:_codeTextField];
    [self.codeErrorView addSubview:self.sendCodeBtn];
    [self setSendCodeBtnStatus:NO];
    [self.codeErrorView addSubview:self.noReceivedBtn]; // 没有收到
    [self.codeErrorView addSubview:self.pwErrorLabel];
}

- (void)changeKeyBoard {
    [_codeTextField becomeFirstResponder];
}

- (void)setSendCodeBtnStatus:(BOOL)judge{
    self.sendCodeBtn.userInteractionEnabled = judge;
    if (judge) {
        [self.sendCodeBtn.layer setBorderColor:kThemeGreenColor.CGColor];
        [self.sendCodeBtn setTitleColor:[UIColor colorWithHex:0x96b5fe]  forState:UIControlStateNormal];
        [self.sendCodeBtn setTitleColor:[UIColor colorWithHex:0x96b5fe]  forState:UIControlStateHighlighted];
    } else {
        [self.sendCodeBtn.layer setBorderColor:[UIColor colorWithHex:0xd8d8d8].CGColor];
        [self.sendCodeBtn setTitleColor:[UIColor colorWithHex:0xd8d8d8]  forState:UIControlStateNormal];
    }
}

//添加移动的view
- (void)addMoveView {
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn  setFrame:CGRectMake(40, 62, kScreenWidth - 80, 45)];
        self.registerBtn.layer.cornerRadius = 5.f;
        self.registerBtn.clipsToBounds = YES;
        self.registerBtn.enabled = NO;
        [self.registerBtn setBackgroundColor:kThemeGreenColor forState:UIControlStateNormal];
        [self.registerBtn setBackgroundColor:kThemeGreenTranslucentColor forState:UIControlStateDisabled];
        [self.registerBtn setTitleColor:[UIColor colorWithHex:0xffffff]  forState:UIControlStateNormal];
        [self.registerBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (self.type == SMSVerifyREGISTERTYPE) {
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        // 设置移动move
        [self.codeErrorView addSubview:self.moveView];
        if (self.showEntry) {
            // 邀请码
            [self.moveView addSubview:self.inviteCodeTextField];
            self.registerBtn.y = self.inviteCodeTextField.bottom + 30;
        } else {
            self.registerBtn.y = 30;
        }
    } else if (self.type == SMSVerifyCodeLoginType) {
        [_registerBtn setTitle:@"登录" forState:UIControlStateNormal];
        // 设置移动move
        [self.codeErrorView addSubview:self.moveView];
        if (self.showEntry) {
            // 邀请码
            [self.moveView addSubview:self.inviteCodeTextField];
            self.registerBtn.y = self.inviteCodeTextField.bottom + 30;
        } else {
            self.registerBtn.y = 30;
        }
    } else if (self.type == SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW) {
        [_registerBtn setTitle:@"完成" forState:UIControlStateNormal];
        self.codeErrorView = [[UIView alloc]initWithFrame:CGRectMake(0, self.setGuideLabel.bottom + 10, kScreenWidth, 337)];
        [self.codeErrorView setBackgroundColor:[UIColor whiteColor]];
        [self.headerView addSubview:self.codeErrorView];
        [self.codeErrorView addSubview:self.pwErrorLabel];
        [self.codeErrorView addSubview:self.moveView];
        [self.moveView addSubview:self.passwordTextField];  // 设置密码
        [self.passwordTextField addSubview:self.clearButton1];
        [self.passwordTextField bringSubviewToFront:self.clearButton1];
        [self.moveView addSubview:self.secondPassworldTextField];   // 第二次密码
        [self.secondPassworldTextField addSubview:self.clearButton2];
        [self.secondPassworldTextField bringSubviewToFront:self.clearButton2];
        self.registerBtn.y = self.secondPassworldTextField.bottom + 30;
    }
    [self.moveView addSubview:self.registerBtn];
}

#pragma mark - UITextFieldDelegate

- (void)changeTextField:(NSNotification *)notification {
    UITextField *textField = [notification object];
    if ([textField.text isEqualToString:@""]) {
        textField.alpha = 1;
        [self showCodeError:NO];
    } else {
        textField.alpha = 0;
    }
    for (int i = 0; i < maxVerifyCodeCount; i++) {
        UILabel *showLabel = (UILabel *)[_showLabelArr objectAtIndex:i];
        if (i <= textField.text.length - 1) {
            showLabel.text = [[textField.text substringFromIndex:i] substringToIndex:1];
        } else {
            showLabel.text = @"";
        }
    }
    if (textField.text.length == maxVerifyCodeCount) {  // 这里不再验证
        self.registerBtn.enabled = YES;
    } else {
        self.registerBtn.enabled = NO;
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *numberString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField == self.passwordTextField || textField == self.secondPassworldTextField) {
        if (numberString.length > 14) {
            return NO;
        } else {
            return YES;
        }
    } else if (textField == self.codeTextField) {
        if (numberString.length > maxVerifyCodeCount) {
            textField.text = [numberString substringToIndex:maxVerifyCodeCount];
            return NO;
        }
    } else if (textField == self.inviteCodeTextField) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.passwordTextField) {
        if (textField.text.length == 0) {
            // 如果密码行没有数据 需要判断再次输入密码位数
            if (self.secondPassworldTextField.text.length < 6 && self.secondPassworldTextField.text.length > 0) {
                [self endEditShowNoReceive:@"密码位数错误, 请输入6-14位密码"];
            } else {
                [self endEditNOShowNoReceive];
            }
        } else if (textField.text.length < 6 && self.secondPassworldTextField.text.length < 6) {
            [self endEditShowNoReceive:@"密码位数错误, 请输入6-14位密码"];
        } else if (textField.text.length < 6 && self.secondPassworldTextField.text.length >= 6) {
            [self endEditShowNoReceive:@"密码位数错误, 请输入6-14位密码"];
        } else if (![self.passwordTextField.text isEqualToString:self.secondPassworldTextField.text] && self.secondPassworldTextField.text.length > 0) {
            [self endEditShowNoReceive:@"两次密码不一致, 请重新输入"];
        } else {
            [self endEditNOShowNoReceive];
        }
    } else if (textField == self.secondPassworldTextField) {
        if (self.secondPassworldTextField.text.length == 0 && self.passwordTextField.text.length == 0) {
            [self endEditNOShowNoReceive];
        } else if (self.secondPassworldTextField.text.length < 6 && self.passwordTextField.text.length == 0) {
            [self endEditShowNoReceive:@"密码位数错误, 请输入6-14位密码"];
        } else if (self.secondPassworldTextField.text.length >= 6 && self.passwordTextField.text.length == 0) {
            [self endEditNOShowNoReceive];
        } else if (self.passwordTextField.text.length < 6 && self.passwordTextField.text.length > 0) {
            [self endEditShowNoReceive:@"密码位数错误, 请输入6-14位密码"];
        } else if (self.passwordTextField.text.length >= 6 && self.secondPassworldTextField.text.length == 0) {
            [self endEditNOShowNoReceive];
        } else  if (![self.secondPassworldTextField.text isEqualToString:self.passwordTextField.text]) {
            [self endEditShowNoReceive:@"两次密码不一致, 请重新输入"];
        } else {
            [self endEditNOShowNoReceive];
        }
    }
}

/**
 *  结束输入之后, 取消显示没有收到
 */
- (void)endEditNOShowNoReceive {
    // 关闭之前需要判断是否显示了没有收到
    if (self.noReceivedBtn.hidden == YES) {
        self.pwErrorLabel.hidden = YES;
        [self  showNoReceive:NO];
    } else {
        self.pwErrorLabel.hidden = YES;
    }
}
/**
 *  结束输入之后, 显示没有收到 或者其他错误
 *
 *  @param title
 */
- (void)endEditShowNoReceive:(NSString *)title{
    self.pwErrorLabel.text = title;
    self.pwErrorLabel.hidden = NO;
    //添加动画了
    [self  showNoReceive:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}


/**
 *  显示密码位数错误 或者没收收到验证码
 *
 *  @param judge YES 拉开 NO 合上
 */
- (void)showNoReceive:(BOOL)judge{
    if (self.pwErrorMark == judge) {
        return;
    }
    self.pwErrorMark = judge;
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    animation.springSpeed = 2;
    animation.springBounciness = 10;
    if (judge) {
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, self.oldMoveViewFrame.origin.y +KTextFieldHeight, self.moveView.width, self.moveView.height)];
    } else {
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(0,self.oldMoveViewFrame.origin.y, self.moveView.width, self.moveView.height)];
    }
    [self.moveView pop_addAnimation:animation forKey:@"frame"];
    
}

/**
 *  显示验证码错误
 *
 *  @param judge YES 拉开 NO 合上
 */
- (void)showCodeError:(BOOL)judge{
    if (self.codeErrorMark == judge) {
        return;
    }
    self.codeErrorMark = judge;
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    animation.springSpeed = 2;
    animation.springBounciness = 10;
    if (judge) {
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, self.oldCodeErrorFrame.origin.y + KTextFieldHeight, self.codeErrorView.width, self.codeErrorView.height)];
        self.headerView.height +=KTextFieldHeight;
    } else {
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, self.oldCodeErrorFrame.origin.y, self.codeErrorView.width, self.codeErrorView.height)];
        self.headerView.height -=KTextFieldHeight;
    }
    [self.codeErrorView pop_addAnimation:animation forKey:@"frame"];
}

- (UIView *)moveView {
    if (!_moveView) {
        if (_type == SMSVerifyREGISTERTYPE || _type == SMSVerifyCodeLoginType) {
            _moveView = [[UIView alloc]initWithFrame:CGRectMake(0, self.codeTextField.bottom, kScreenWidth,self.codeErrorView.height-20-KTextFieldHeight)];
        } else if (_type == SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW){
            _moveView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.codeErrorView.height-20-KTextFieldHeight)];
        }
        self.oldMoveViewFrame = _moveView.frame;
        [_moveView setBackgroundColor:[UIColor whiteColor]];
    }
    return _moveView;
}

- (UITextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, kScreenWidth - 80, 50)];
        _passwordTextField.delegate = self;
        _passwordTextField.placeholder = @"设置密码为6-14位";
        _passwordTextField.secureTextEntry = YES;
        [_passwordTextField setTextColor:kThemeGreenColor];
        [_passwordTextField setLineWithDirection:TGLINEBOTTOM Color:[UIColor colorWithHex:0xe5e5e5] Height:kTYHSingleLine Width:0];
        [_passwordTextField setFont:[UIFont systemFontOfSize:16]];
    }
    return _passwordTextField;
}

- (UITextField *)inviteCodeTextField {
    if (!_inviteCodeTextField) {
        _inviteCodeTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 0, kScreenWidth - 80, 16)];
        _inviteCodeTextField.placeholder = @"邀请码(非必填)";
        _inviteCodeTextField.delegate = self;
        [_inviteCodeTextField setKeyboardType:UIKeyboardTypeNumberPad];
        [_inviteCodeTextField setTextColor:kThemeGreenColor];
        [_inviteCodeTextField  setLineWithDirection:TGLINEBOTTOM Color:[UIColor colorWithHex:0xe5e5e5]  Height:kTYHSingleLine Width:0];
        [_inviteCodeTextField setFont:[UIFont systemFontOfSize:16]];
    }
    return _inviteCodeTextField;
}

- (UIButton *)sendCodeBtn {
    if (!_sendCodeBtn) {
        // 添加发送的btn
        _sendCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendCodeBtn setFrame:CGRectMake(kScreenWidth - 30 - 96, _codeTextField.height, 96, 30)];
        _sendCodeBtn.centerY = _codeTextField.centerY;
        [_sendCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_sendCodeBtn.layer setCornerRadius:15];
        self.sendCodeBtn.clipsToBounds = YES;
        [_sendCodeBtn addTarget:self action:@selector(sendSmsCode) forControlEvents:UIControlEventTouchUpInside];
        [_sendCodeBtn setBackgroundColor:kOPBackgroundColor forState:UIControlStateNormal];
        [_sendCodeBtn setTitleColor:kThemeGreenColor forState:UIControlStateNormal];
        [_sendCodeBtn setTitleColor:[UIColor colorWithHex:0xd2d2d2] forState:UIControlStateDisabled];
        [_sendCodeBtn setTitle:@"60秒后重发" forState:UIControlStateNormal];
    }
    return _sendCodeBtn;
}

- (UIButton *)noReceivedBtn {
    if (!_noReceivedBtn) {
        _noReceivedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_noReceivedBtn setFrame:CGRectMake(kScreenWidth - 25 - 80, self.codeTextField.bottom + 15, 80, 20)];
        [_noReceivedBtn setTitle:@"没收到?" forState:UIControlStateNormal];
        [_noReceivedBtn.titleLabel setFont:LABELFONT];
        [_noReceivedBtn.titleLabel setTextAlignment:NSTextAlignmentLeft];
        _noReceivedBtn.hidden = YES;
        [_noReceivedBtn setTitleColor:kThemeGreenColor  forState:UIControlStateNormal];
    }
    return _noReceivedBtn;
}

- (UILabel *)pwErrorLabel {
    if (!_pwErrorLabel) {
        _pwErrorLabel = [[UILabel alloc] init];
        if (self.type == SMSVerifyREGISTERTYPE || self.type == SMSVerifyCodeLoginType) {
            [_pwErrorLabel setFrame:CGRectMake(40, self.codeTextField.bottom + 15, kScreenWidth - 135, 20)];
        } else if (self.type== SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW) {
            [_pwErrorLabel setFrame:CGRectMake(40, 0,  kScreenWidth - 135, 20)];
        }
        _pwErrorLabel.text = @"密码位数错误, 请输入6-14位密码";
        _pwErrorLabel.font = LABELFONT;
        [_pwErrorLabel setTextColor:[UIColor colorWithHex:0xff3c37] ];
        _pwErrorLabel.hidden = YES;
    }
    return _pwErrorLabel;
}

- (UIScrollView *)headerView {
    if (!_headerView) {
        _headerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.scrollEnabled = NO;
        [self.view addSubview:_headerView];
    }
    return _headerView;
}


- (void)keyBoardShow:(NSNotification *)noti{
    //需要知道那个textField是第一响应者
    // NSDictionary *info = [noti userInfo];
    // NSValue *value = [info objectForKeySafely:UIKeyboardFrameEndUserInfoKey];
    // CGSize keyboardSize = [value CGRectValue].size;
    // CGFloat keyboardHeight = keyboardSize.height;
    CGFloat textFieldY = 64 + kScreenWidth/2.55 + 30 +10;
    if (self.type == SMSVerifyREGISTERTYPE  || self.type == SMSVerifyCodeLoginType) {
        if ([self.codeTextField isFirstResponder]) {
            if (self.codeErrorMark == YES) {
                textFieldY += KTextFieldHeight;
            }
            textFieldY += KTextFieldHeight;
        } else if (_inviteCodeTextField){
            if ([self.inviteCodeTextField isFirstResponder]) {
                textFieldY += KTextFieldHeight;
            }
        }
    } else if (self.type == SMSVerifyVERITYTYPE || self.type == SMSVerifyCodeLoginNeedSetPW){
        if ([self.passwordTextField isFirstResponder]) {
            if (self.pwErrorMark == YES) {
                textFieldY += KTextFieldHeight;
            }
            textFieldY += (KTextFieldHeight);
        } else if ([self.secondPassworldTextField isFirstResponder]){
            if (self.pwErrorMark == YES) {
                textFieldY += KTextFieldHeight;
            }
            textFieldY += (KTextFieldHeight+KTextFieldHeight);
        }
    }
}

- (void)keyBoardHide:(NSNotification *)noti{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITextField *)secondPassworldTextField {
    if (!_secondPassworldTextField) {
        _secondPassworldTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, self.passwordTextField.bottom, kScreenWidth - 80, 50)];
        _secondPassworldTextField.delegate = self;
        _secondPassworldTextField.placeholder = @"确认密码";
        [_secondPassworldTextField setTextColor:kThemeGreenColor];
        _secondPassworldTextField.secureTextEntry = YES;
        [_secondPassworldTextField setLineWithDirection:TGLINEBOTTOM Color:[UIColor colorWithHex:0xe5e5e5] Height:kTYHSingleLine Width:0];
        [_secondPassworldTextField setFont:[UIFont systemFontOfSize:16]];
    }
    return _secondPassworldTextField;
}

@end
